#include "ThreadManager.h"

ThreadManager::ThreadManager()
{

	m_threadA = new CThreadA(m_framework);
	m_threadB = new CThreadB(m_framework);
	m_mqttsub = new CMQTTSub(m_framework);
	m_portmanager = new CPortManager(m_framework);
	m_mqttpub = new CMQTTPub(m_framework);

	m_threadA->setID(THREAD_A);
	m_threadB->setID(THREAD_B);
	m_mqttsub->setID(THREAD_MQTTSUB);
	m_portmanager->setID(THREAD_PORT_MANAGER);
	m_mqttpub->setID(THREAD_MQTTPUB);

	setThreadID(THREAD_A, m_threadA->GetAddress());
	setThreadID(THREAD_B, m_threadB->GetAddress());
	setThreadID(THREAD_MQTTSUB, m_mqttsub->GetAddress());
	setThreadID(THREAD_PORT_MANAGER, m_portmanager->GetAddress());
	setThreadID(THREAD_MQTTPUB, m_mqttpub->GetAddress());

	//int timeid1 = 100;
	//int timeid0 = 20;
	//m_timeid.push_back(timeid0);
	//m_timeid.push_back(timeid1);
	//m_threadA->setTimer(5000, TIME_ID_ONE);
	//m_threadA->setTimer(1000, TIME_ID_TWO);

	//CDataCenter::instance()->m_num = 1;

	m_mqttpub->setTimer(15000, MQTTPUB_TIME_ID);

	m_num = 100;
	start();
}

void ThreadManager::start()
{
	//threadAͳźŸthreadB
	//m_threadA->send2ThreadbyID(0, THREAD_B);
	//m_threadA->start();
	m_mqttpub->start();
	m_portmanager->start();
	m_mqttsub->start();
	//m_framework.Send(1, m_threadA->GetAddress(), m_mqttsub->GetAddress());
	//m_framework.Send(2, m_threadA->GetAddress(), m_mqttsub->GetAddress());
	//m_framework.Send(1, m_threadA->GetAddress(), m_mqttsub->GetAddress());
}

void ThreadManager::stop()
{

}

int ThreadManager::setThreadID(int id, Theron::Address address)
{
	std::pair<std::map<int, Theron::Address>::iterator, bool> Insert_Res;
	Insert_Res = CDataCenter::instance()->ID_ADDRESS_MAP.insert(std::pair<int, Theron::Address>(id, address));
	if (Insert_Res.second == true)
	{
		printf("Insert producer address to map successfully.\n");
		return 0;
	}

	printf("Insert producer address to map failed.\n");
	return -1;
}

